//
//  RXMessageVC.swift
//  RXSwiftDemo1
//
//  Created by mac on 2025/3/7.
//

import UIKit
import RxSwift
import RxCocoa

class RXMessageVC: UIViewController {

    @IBOutlet weak var reciveLabel: UILabel!
    @IBOutlet weak var showReciveLabel: UILabel!
    @IBOutlet weak var sendTimeButton: UIButton!
    @IBOutlet weak var electricityButton: UIButton!
    @IBOutlet weak var inversionButton: UIButton!
    @IBOutlet weak var findDeviceButton: UIButton!
    @IBOutlet weak var deviceStateButton: UIButton!
    @IBOutlet weak var countButton: UIButton!
    @IBOutlet weak var realTimeCountButton: UIButton!
    @IBOutlet weak var remotePowerOffButton: UIButton!
    @IBOutlet weak var remoteRestartButton: UIButton!
    @IBOutlet weak var setDeviceMacAddressButton: UIButton!
    
    
    private lazy var msgManager = XCBluetoothMessageManager.instance
    
    private let blueManager = XCBluetoothManager.instance
    
    private let disposeBag: DisposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        msgManager.readBluetoothMsg()
            .compactMap({ String(data: $0, encoding: .utf8) })
            .subscribe(onNext: { msg in
                print("✅ 接受的消息为: \(msg)")
            })
            .disposed(by: disposeBag)
        
//        blueManager.connectedPeripheral?.rx.didUpdateValueForCharacteristic
//            .subscribe(onNext: { characteristic in
//                
//                guard let value = characteristic.value else {
//                    print("特征值更新成功，但 value 为 nil")
//                    return
//                }
//                
//                let msg = String(data: value, encoding: .utf8)
//                print("特征值发生变化时回调: \(msg ?? "无法解析为字符串")")
//            })
//            .disposed(by: disposeBag)
        
        sendTimeButton.rx.tap
            .subscribe(onNext: {
                print("✅ 下发时间成功")
                let dispose = DisposeBag()
                self.msgManager.sendTimeToDevice()
                    .subscribe()
                    .disposed(by: dispose)
            })
            .disposed(by: disposeBag)
        
        electricityButton.rx.tap
            .subscribe(onNext: {
                print("✅ 获取设备电量成功")
                let dispose = DisposeBag()
                self.msgManager.getDeviceElectricity()
                    .subscribe()
                    .disposed(by: dispose)
            })
            .disposed(by: disposeBag)
    }

}
